#include "func.h"
#include "mysql.h"



static void error_quit(char *msg);



void error_quit(char *msg)
{
    perror(msg);
    exit(-2);
}

void GenerateRandomStr(char * str)  // 生成随机字符串，用于生成随机salt值
{
    int i, flag;
    srand(time(NULL));
    for(i = 0; i < 10; i++)
    {
        flag = rand() % 3;
        switch(flag)
        {
            case 0: str[i] = rand() % 26 + 'a'; break;
            case 1: str[i] = rand() % 26 + 'A'; break;
            case 2: str[i] = rand() % 10 + '0'; break;
        }
    }
    str[10] = '\0';
    printf("%s\n", str);
}

char * getpath(char * path)     // 获取当前工作目录
{
    getcwd(path, 255);
    printf("path:%s\n", path);
    int left = 0;
    char buff[255];
    for(int i = 0; i < strlen(path); i++)
        if(path[i] == '/')
            left = i;
    for(int i = left; i < strlen(path); i++)
        buff[i - left] = path[i];
    printf("buff:%s\n", buff);
    buff[strlen(path) - left] = '\0';
    printf("buff:%s\n", buff);
    for(int i = 0; i < strlen(buff); i++)
        path[i] = buff[i];
    path[strlen(buff)] = '\0';
    printf("path:%s\n", path);
    
    char * c = "/";
    path = strcat(path, c);

    printf("path:%s\n", path);
    return path;
}

int passwdCheck(MYSQL * conn, char *username, char * passwd)    // 验证用户名和密码
{


      MYSQL sql;
      mysql_init(&sql);
      MYSQL *conn1 = mysql_real_connect(&sql, "localhost", "root", "machine", "NetWork", 3306, NULL, 0);
      if (conn1 == NULL)
      {
          printf("Connect Mysql Fail.\n");                                                                                                                   
          exit(1);
      }




    printf("passwd check begin\n");
    char * salt;
    char select1[255] = "select username from User where username = '";
    char * query1 = strcat(select1, username);
    char * temp = "\';";
    query1 = strcat(query1, temp);
    printf("%s\n", query1);
    if(mysql_query(conn, query1) != 0)
    {
        printf("unknow user\n");
        exit(1);
    }

    MYSQL_RES *query1Res = mysql_store_result(conn);
    mysql_free_result(query1Res);

    printf("passwd:%s\n", passwd);
    
    char select2[255] = "select salt from User where username = '";
    char * query2 = strcat(select2, username);
    query2 = strcat(query2, temp);
    printf("%s\n", query2);
    mysql_query(conn1, query2);
    MYSQL_RES * saltRes = mysql_store_result(conn1);
    MYSQL_ROW saltRow = mysql_fetch_row(saltRes);
    printf("salt:%s\n", saltRow[0]);

    char select3[255] = "select cryptpasswd from User where username = '";
    char * query3 = strcat(select3, username);
    query3 = strcat(query3, temp);
    printf("%s\n", query3);
    ERROR(conn, mysql_query(conn1, query3));
    MYSQL_RES * pwdRes = mysql_store_result(conn1);
    MYSQL_ROW pwdRow = mysql_fetch_row(pwdRes);
    printf("cryptpwd:%s\n", pwdRow[0]);

    if(strcmp(pwdRow[0], crypt(passwd, saltRow[0])) == 0)
        printf("check success\n");
    else
    {
        printf("check failed\n");
        return -1;
    }
    mysql_free_result(saltRes);
    mysql_free_result(pwdRes);
    return 0;
}

int AddUserToMysql(MYSQL * conn, char * username, char * passwd)    // 添加新用户
{
    printf("add user begin\n");
    char * temp = "\';";
    char select1[255] = "select username from User where username = '";
    char * query = strcat(select1, username);
    query = strcat(query, temp);
    printf("%s\n", query);
    
    mysql_query(conn, query);
    MYSQL_RES * queryRes = mysql_store_result(conn);
    MYSQL_ROW queryRow = mysql_fetch_row(queryRes);
  //  if(strcmp(queryRow[0], username) == 0)
  //  {
  //      printf("user:%s aleady existed\n", username);
  //      exit(1);
  //  }
    mysql_free_result(queryRes); 
    char salt[64] = {0};
    GenerateRandomStr(salt);
    printf("salt:%s\n", salt);
    printf("passwd:%s\n", passwd);

    char * cryptpasswd = crypt(passwd, salt);
    printf("cryptpasswd:%s\n", cryptpasswd);
    char path[255] = {0};
    char * path1 = getpath(path);
    printf("path1:%s\n", path1);
    InsertUser(conn, username, salt, cryptpasswd, path1);
    printf("The new user is successfully added\n");
    return 0;
}





